﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BomberManServer.Gaming.Object.Mapping.Items;
using BomberManServer.Gaming.Time;
using BomberManServer.Utils;

namespace BomberManServer.Gaming.Object.Mapping
{
    public class Slab : Block
    {
        bool active;
        private Trap trap;
        private Trigger timerReset;

        public Slab(Block old, Trap trap) : base(old.coordinates.xBlock, old.coordinates.yBlock, None.instance)
        {
            active = true;
            this.trap = trap;
            timerReset = new Trigger(Constants.instance.actions_per_second * Constants.instance.timerSlabReset, reset);
        }

        public override void reactionWalk(Avatar avatar)
        {
            if(active) activate(avatar);
            base.reactionWalk(avatar);
        }

        private void activate(Avatar avatar)
        {
            active = false;
            avatar.game.sender.onSlabOff(this.coordinates);
            trap.explode(avatar);
            // freeze the slab for 1 second
            trap.scheduler.schedule(timerReset);
            
        }

        private void reset()
        {
            active = true;
            trap.sender.onSlabOn(this.coordinates);
            trap.scheduler.remove(timerReset);
        }
    }
}
